package com.java46.admin.user.action;

import com.java46.admin.base.action.BaseAction;
import com.java46.admin.user.entity.UsersEntity;
import com.java46.admin.user.service.UserServiceT;
import com.java46.admin.user.util.Page;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class UserActionT extends BaseAction {
    UserServiceT userServiceT = new UserServiceT();
    @Action(value = "queryUser",results={
            @Result(name="success",location="/detail/userList.jsp")
    })
    /**
     * 查询所有用户
     */
    public String query_User(){

        HttpServletRequest request = ServletActionContext.getRequest();
        try {
            //从页面得到当前页码和微信账号
            String pageNo = request.getParameter("pageNo");
            String openId = request.getParameter("openId");
            //如果当前页码为空，让它的初始值为1
            if (pageNo == null) {
                pageNo = "1";
            }
            //模糊搜索的条件
            if(openId!=null){
                request.setAttribute("openId", openId);
            }
            Page page = userServiceT.queryForPage(Integer.valueOf(pageNo), 5,openId);
            request.setAttribute("page", page);
            List<UsersEntity> course = page.getList();
            request.setAttribute("lists", course);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }
    /**
     * 删除用户
     */
    private String rOpenId;

    public String getrOpenId() {
        return rOpenId;
    }

    public void setrOpenId(String rOpenId) {
        this.rOpenId = rOpenId;
    }

    @Action(value = "removeUser",results={
            @Result(name="success" ,type="chain",location="queryUser")
    })
    public String removeUser(){
        int i = userServiceT.removeUser(rOpenId);
        if(i>=1){
            return  "success";
        }else{
            return "input";
        }
    }

}
